package com.xwaf.platform.web.controller.gateway;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.xwaf.platform.common.api.CommonResponse;
import com.xwaf.platform.common.api.constants.BaseConstants;
import com.xwaf.platform.common.api.entity.ELUser;
import com.xwaf.platform.common.web.BaseController;
import com.xwaf.platform.system.api.entity.DataBase;
import com.xwaf.platform.system.api.entity.gateway.Token;
import com.xwaf.platform.system.api.service.gateway.TokenService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * <p>
 * <p>
 * 
 * @author 李聪 <br>
 * @email xwaf_love@yeah.net <br>
 * @since JDK 1.8<br>
 * @date 2019-6-6 10:58:03 <br>
 * @see 无<br>
 *      Copyright (c) 2019, xwaf_love@yeah.net All Rights Reserved.<br>
 */
@RestController
@Api(tags = "Token")
@RequestMapping(value = "/m/token/")
public class TokenController extends BaseController {

	@Autowired
	private TokenService tokenService;

	@ApiOperation(value = "分页查询", response = Token.class)
	@PostMapping(value = "page", produces = "application/json;charset=UTF-8")
	public CommonResponse page(@RequestParam("currentPage") String currentPage,
			@RequestParam("pageSize") String pageSize, @RequestBody Token token) {
		CommonResponse resp = CommonResponse.createCommonResponse();
		if (!StringUtils.isNotBlank(currentPage) & !StringUtils.isNotBlank(pageSize)) {
			currentPage = "1";
			pageSize = "10";
		}
		Page<Token> page = new Page<Token>(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
		Page<Token> result = tokenService.selPageList(page, token);
		resp.setData(result);
		return resp;
	}

	@ApiOperation(value = "根据条件查询", notes = "根据条件查询信息", response = Token.class, httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	@GetMapping(value = "tokens", produces = "application/json;charset=UTF-8")
	public CommonResponse list(@RequestParam("name") String name) {
		Wrapper<Token> wrapper = new EntityWrapper<Token>();
		wrapper.like("name", name);
		List<Token> list = tokenService.selectList(wrapper);
		return CommonResponse.createCommonResponse(list);
	}

	@ApiOperation(value = "新增", notes = "新增信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
	@PostMapping(value = "add", produces = "application/json;charset=UTF-8")
	public CommonResponse addToken(@RequestBody Token token) {
		ELUser loginUser = getLoginUser();
		if (loginUser != null) {
			token.setCreateBy(loginUser.getId());
			token.setUpdateBy(loginUser.getId());
		}
		token.setId(DataBase.getDataBaseId(DataBase.TN));
		boolean isAdded = tokenService.insert(token);
		if (isAdded) {
			return CommonResponse.createCommonResponse(token);
		} else {
			return CommonResponse.createCustomCommonResponse(BaseConstants.FAIL.getCode(), "新增失败!");
		}
	}

	@ApiOperation(value = "修改", notes = "修改信息", httpMethod = "PUT", produces = MediaType.APPLICATION_JSON_VALUE)
	@PutMapping(value = "update/{tokenId}", produces = "application/json;charset=UTF-8")
	public CommonResponse updateToken(@PathVariable("tokenId") String tokenId, @RequestBody Token token) {
		ELUser loginUser = getLoginUser();
		if (loginUser != null) {
			token.setUpdateBy(loginUser.getId());
		}
		token.setId(tokenId);
		boolean isUpdated = tokenService.updateById(token);
		if (isUpdated) {
			return CommonResponse.createCommonResponse();
		} else {
			return CommonResponse.createCustomCommonResponse(BaseConstants.FAIL.getCode(), "修改失败!");
		}
	}

	@ApiOperation(value = "删除", notes = "删除信息", httpMethod = "DELETE", produces = MediaType.APPLICATION_JSON_VALUE)
	@DeleteMapping(value = "delete/{tokenIds}", produces = "application/json;charset=UTF-8")
	public CommonResponse delById(@PathVariable("tokenIds") List<String> tokenIds) {
		// boolean isDel = tokenService.deleteBatchIds(tokenIds);
		boolean isDel = true;
		if (isDel) {
			return CommonResponse.createCommonResponse();
		} else {
			return CommonResponse.createCustomCommonResponse(BaseConstants.FAIL.getCode(), "删除失败!");
		}
	}

	@ApiOperation(value = "查看详情", notes = "查看详情信息", response = Token.class, httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	@GetMapping(value = "info/{tokenId}", produces = "application/json;charset=UTF-8")
	public CommonResponse getInfoById(@PathVariable("tokenId") String tokenId) {
		Token token = tokenService.selectById(tokenId);
		return CommonResponse.createCommonResponse(token);
	}
}
